package org.ligson.fwc.api.enums;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
public class BaseEnum {
    private int code;
    private String key;
    private String msg;
    private static Map<String, BaseEnum> codeMap = new HashMap<>();
    private static Map<String, BaseEnum> keyMap = new HashMap<>();
    private static List<BaseEnum> values = new ArrayList<>();


    @SuppressWarnings("unchecked")
    public static <T extends BaseEnum> T getByCode(int code, Class<T> tClass) {
        String codeKey = tClass.getName() + "_code_" + code;
        return (T) codeMap.get(codeKey);
    }

    @SuppressWarnings("unchecked")
    public static <T extends BaseEnum> List<T> values() {
        return (List<T>) values;
    }

    public BaseEnum(int code, String key, String msg) {
        this.code = code;
        this.key = key;
        this.msg = msg;
        String className = this.getClass().getName();
        codeMap.put(className + "_code_" + code, this);
        keyMap.put(className + "_key_" + key, this);
        values.add(this);
    }
}
